<template>
  <!-- 协催管理-法务催收-页面 -->
  <section class="page assist-legal-collection">
    <data-form :model="searchModel" @onSearch="refreshData" @handleResetForm="follFeedback">
      <template slot="inputs">
        <el-form-item label="订单号" prop="caseNumber">
          <el-input v-model="searchModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="searchModel.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="searchModel.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="searchModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="searchModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="searchModel.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="searchModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="searchModel.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="searchModel.bankName"></el-input>
        </el-form-item>
        <el-form-item label="机构" prop="mainDeptName">
          <el-input v-model="searchModel.mainDeptName"></el-input>
        </el-form-item>
        <el-form-item label="催收员" prop="currentCollectorRealName">
          <el-input v-model="searchModel.currentCollectorRealName" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="协催机构" prop="deptId">
          <el-cascader v-model="searchModel.deptId" :options="departmentData" :props="{value:'id',label:'deptName'}" change-on-select :show-all-levels="false" clearable></el-cascader>
        </el-form-item> -->
        <el-form-item label="协催员" prop="assistCollectorName">
          <el-input v-model="searchModel.assistCollectorName" clearable></el-input>
        </el-form-item>
        <el-form-item label="贷款金额" prop="loanAmount">
          <number-range v-model="searchModel.loanAmount"></number-range>
        </el-form-item>
        <el-form-item label="贷款期数" prop="loanPeriod">
          <number-range v-model="searchModel.loanPeriod"></number-range>
        </el-form-item>
        <el-form-item label="催收总金额" prop="overdueAmount">
          <number-range v-model="searchModel.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="searchModel.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="searchModel.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNum">
          <el-input v-model="searchModel.plateNum"></el-input>
        </el-form-item>
        <el-form-item label="协催来源" prop="collectionType">
          <el-select v-model="searchModel.collectionType" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in departmentType" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="协催审批时间" prop="flowInTime">
          <el-date-picker type="daterange" :editable="false" v-model="searchModel.flowInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="协催剩余天数" prop="personalCollectionRemainingDays">
          <number-range v-model="searchModel.personalCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :page="pageService" showCaseMark>
      <template slot="append">
        <case-marker @change-color="onColorChange" :selectionList="selectionList"></case-marker>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="mainDeptName" label="机构" show-overflow-tooltip :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="currentCollectorRealName" label="催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="deptName" label="协催机构" show-overflow-tooltip :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="assistCollectorName" label="协催员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.loanAmount)">
        </el-table-column>
        <el-table-column prop="loanPeriod" label="贷款期数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueStage" label="逾期阶段" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sumMoney" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="plateNum" label="车牌号" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="collectionType" label="协催来源" :formatter="(row) => $filter.dictConvert(row.collectionType,'DepartmentType')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="flowInTime" label="协催审批时间" :formatter="(row) => $filter.dateTimeFormat(row.flowInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="personalCollectionRemainingDays" label="协催剩余天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
      </template>
    </data-box>
    <el-dialog :title="`${applyType.name}申请`" width="800px" :visible.sync="dialog.apply" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body>
      <apply :caseIds="selectedIds" :fromChannel="caseChannel" :applyType="applyType.value" @close="dialog.apply = false" ref="apply-circulation"></apply>
    </el-dialog>
    <el-dialog title="重新分案" :visible.sync="dialog.ManualDivision" width="800px" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <case-allot again :fromChannel="caseChannel" @refreshList="refreshData" @close="dialog.ManualDivision=false"></case-allot>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import NumberRange from "~/components/common/number-range.vue";
import { Getter, Mutation, namespace, State } from "vuex-class";
import { CaseAssistService } from "~/services/domain-service/case-assist.service.ts";
import UserCaseAllot from "~/components/distribution-common/user-case-allot.vue";
import CaseMarker from "~/components/common/case-marker.vue";
import { ApplyType, CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import Apply from "~/components/visit-case/apply.vue";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";

const CollectionExecutionModule = namespace("collection-execution");
const AllotModule = namespace("case-allot");

@Auth(1525)
@Layout("workspace")
@Component({
  components: {
    GetLabelName,
    UserCaseAllot,
    NumberRange,
    CaseMarker,
    Apply
  }
})
export default class AssistLegalCollection extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(CaseAssistService)
  private caseAssistService: CaseAssistService;
  @Getter
  private visitDepartmentDataNoRoot;
  @Getter
  private departmentData;
  @Mutation
  private updateCollectionType;
  @CollectionExecutionModule.Action private openCollectionExecution;
  @CollectionExecutionModule.Mutation private updateCollectionSign;
  private caseChannel = CASE_CHANNEL.assist_legal;
  private applyType = ApplyType.assistLeave;
  @AllotModule.Mutation private updateCaseIds;
  @AllotModule.Mutation private updateExtData;
  @State private principalList: any;

  // private caseMark: any = "";
  private searchModel: any = {
    caseNumber: "",
    batchNumber: "",
    principalId: "",
    personalName: "",
    idCard: "",
    loanDate: [],
    mobileNo: "",
    partnerName: "",
    bankName: "",
    mainDeptName: "",
    currentCollectorRealName: "",
    loanAmount: { min: "", max: "" },
    loanPeriod: { min: "", max: "" },
    overdueAmount: { max: "", min: "" },
    businessAddress: "",
    boardAddress: "",
    plateNum: "",
    collectionType: "",
    flowInTime: [],
    personalCollectionRemainingDays: { min: "", max: "" },
    deptId: [],
    assistCollectorName: "",
    flag: false,
  };
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private dialog: any = {
    ManualDivision: false,
    apply: false
  };

  private mounted() {
    this.refreshData();
  }

  private activated() {
    this.refreshData();
  }

  //过滤协催来源
  private get departmentType() {
    // 要过滤的字段
    const disabledDepartmentTypes: any = ["WAIT_DISTRIBUTE", "UNIVERSAL", "OUTER", "STOP", "LAW", "CHEAT", "VERIFIER", "REPAIR", "GRAB_CASE", "RECOVERED"]
    return this.$dict.getDictData('DepartmentType').filter(v => !disabledDepartmentTypes.includes(v.code))
  }

  private refreshData() {
    this.caseAssistService
      .getAllLegalCaseAssist(
        this.searchModel,
        this.pageService,
        this.sortService
      )
      .subscribe(data => {
        this.dataSet = data;
        this.selectionList = [];
      });
  }

  /**
   * 留案申请
   */
  private leaveApply() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要分配的案件");
      return;
    }
    this.dialog.apply = true;
  }

  /**
   * 重新分案
   */
  private manualDivision() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要分配的案件");
      return;
    }
    this.updateCaseIds(this.selectionList.map(v => v.caseId))
    const caseAssistIds = this.selectionList.map(v => v.id)
    this.updateExtData({ caseAssistIds })
    this.dialog.ManualDivision = true;
  }

  /**
   * 进入编辑执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(
      row.caseId,
      {
        menuChannel: MENU_CHANNEL.Assist_legal,
        hiddenCaseList: true,
        hiddenAction: row.assistStatus === "ASSIST_OVER",
        otherData: {
          assistId: row.id
        }
      });
  }

  /**
   * 重置numberRange
   */
  private follFeedback() {
    this.searchModel.overdueAmount = { min: "", max: "" };
    this.searchModel.overdueDays = { min: "", max: "" };
    this.searchModel.caseMoney = { min: "", max: "" };
  }

  private get selectedIds() {
    return this.selectionList.map(v => v.caseId);
  }

  private onColorChange(val) {
    const colorModel: any = {
      caseAssistIds: this.selectionList.map(v => v.id),
      caseMark: val
    };
    this.caseAssistService
      .markCaseAssistColor(colorModel)
      .subscribe(this.refreshData);
  }
}
</script>

<style lang="less">
</style>
